package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //@Controller + @ResponseBody的结合体
@RequestMapping("/emp") //暴露公共路径
@Api(tags = "员工管理接口")  //swagger API
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/findManagers")
    @ApiOperation(value = "查询所有的经理") //swagger API
    public AjaxResult findManagers(){

        //查询
        List<Employee> list = employeeService.findManagers();
        //返回
        return AjaxResult.createSuccess(list);
    }
    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有的员工") //swagger API
    public AjaxResult list(){

        //查询
        List<Employee> list = employeeService.findAll();
        //返回
        return AjaxResult.createSuccess(list);
    }
    /**
     * 查询员工分页
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "查询员工分页") //swagger API
    public PageList<Employee> findPage(@RequestBody EmployeeQuery query){
        //查询
        return employeeService.findPage(query);
    }


    /**
     * 新增/修改  ，根据id区分
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增修改员工，新增没有id，修改有id") //swagger API
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){

        try {
            if (employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }

    }
    /**
     * 删除
     * @return
     */
    @ApiOperation(value = "根据id删除员工对象") //swagger API
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            employeeService.delete(id);
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }
    //    批量删除
    @PostMapping("/batchRemove")
    @ApiOperation(value = "根据id删除部门对象")
    public AjaxResult deleteDatch(@RequestBody EmployeeQuery query){
        employeeService.batchRemove(query);
        return AjaxResult.createSuccess();
    }

}
